package com.shu.logsearch.core.boot;

import cn.hutool.core.io.FileUtil;
import com.shu.logsearch.config.FileProperties;
import com.shu.logsearch.es.model.LogDoc;
import com.shu.logsearch.es.service.ESRestService;
import com.shu.logsearch.es.service.TikaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Arrays;

/**
 * 加载日志文件
 *
 * @author Tree Cai
 * @date 2019/7/1
 */
@Order(2)
@Component
public class LoadLogFileApplicationRunner implements ApplicationRunner {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private FileProperties fileProperties;
    @Autowired
    private TikaService tikaService;
    @Autowired
    private ESRestService esRestService;

    @Override
    public void run(ApplicationArguments args) throws Exception{
//        String jsonFilePath = "json/docIndex.json";
//        String jsonSource = FileUtil.readString(jsonFilePath,"UTF-8");
//        if (esRestService.existIndex(LogDoc.INDEX_NAME))
//            esRestService.deleteIndex(LogDoc.INDEX_NAME);
//        if (!esRestService.addIndex(LogDoc.INDEX_NAME,3,1,jsonSource))
//            return;
//
//        logger.info("开始加载文件到ES！");
//        String paths = fileProperties.getPath();
//        logger.info("文件目录：" + paths);
//        for (String path : paths.split(",")) {
//            File directory = FileUtil.file(path);
//            System.out.println(directory.exists());
//            System.out.println(directory.isDirectory());
//            if (directory.exists() && directory.isDirectory()) {
//                esRestService.indexDocs(LogDoc.INDEX_NAME, tikaService.parserFilesToDoc(Arrays.asList(directory.listFiles())));
//            }
//        }
//        logger.info("成功加载文件到ES！");
    }
}
